package com.kunteng.ynhxksh.service;

/**
 * Created By ShiYun with IntelliJ IDEA on 2018/4/2 10:47
 *
 * @author: ShiYun
 * @Email: 515867726@qq.com
 * @Date: 2018/4/2
 * To change this template use File | Settings | File Templates.
 */

import com.kunteng.ynhxksh.dao.SBGKDao;
import com.kunteng.ynhxksh.mapper.T_POS_JYXXMapper;
import com.kunteng.ynhxksh.mapper.T_POS_MANAGEMapper;
import com.kunteng.ynhxksh.util.DateUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 设备管控服务
 */
@Service
public class SBGKService implements SBGKDao {

    Logger logger = LoggerFactory.getLogger(SBGKService.class);

    @Autowired
    private T_POS_MANAGEMapper t_pos_manageMapper;

    @Autowired
    private T_POS_JYXXMapper t_pos_jyxxMapper;

    @Override
    public JSONObject querySBNums() {
        String dateStr = DateUtils.getDate_yyyy_MM_dd(new Date());
        logger.info("querySBNums {}", dateStr);
        JSONObject one = new JSONObject();
        one.put("total", t_pos_manageMapper.countPOS_total());
        one.put("active", t_pos_manageMapper.countPOS_active());
        one.put("online", t_pos_manageMapper.countPOS_online());
        one.put("hasjy", t_pos_manageMapper.countPOS_hasJY_byDate(dateStr));
        return one;
    }

    @Override
    public JSONArray querySBAweekActiveQS() {
        JSONArray jsonArray = new JSONArray();
        logger.info("querySBAweekActiveQS");
        for (int i = 0; i < 7; i++) {
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.DATE, -i);
            String dateStr = DateUtils.getDate_yyyy_MM_dd(calendar.getTime());
            int num = t_pos_manageMapper.countPOS_active_byDate(dateStr);
            JSONObject one = new JSONObject();
            one.put("date", dateStr);
            one.put("num", num);
            jsonArray.add(one);
        }
        return jsonArray;
    }

    @Override
    public JSONArray queryTodayZFFS() {
        JSONArray jsonArray = new JSONArray();
        String dateStr = DateUtils.getDate_yyyy_MM_dd(new Date());
        logger.info("queryTodayZFFS {}", dateStr);
        List<Map> jy = t_pos_jyxxMapper.queryJYByDate(dateStr);
        double jye_total = ((BigDecimal) jy.get(0).get("jye")).doubleValue();
        List<Map> list = t_pos_manageMapper.countJYE_byZFFS(dateStr);
        for (Map map : list) {
            int zffs = ((Long) map.get("zffs")).intValue();
            double jyze = ((BigDecimal) map.get("jyze")).doubleValue();
            JSONObject one = new JSONObject();
//            支付途径[0-现金/1-银行卡/2-微信/3-支付宝]
            switch (zffs) {
                case 0:
                    one.put("bm", 0);
                    one.put("mc", "现金");
                    break;
                case 1:
                    one.put("bm", 1);
                    one.put("mc", "银行卡");
                    break;
                case 2:
                    one.put("bm", 2);
                    one.put("mc", "微信");
                    break;
                case 3:
                    one.put("bm", 3);
                    one.put("mc", "支付宝");
                    break;
                default:
                    one.put("bm", 0);
                    one.put("mc", "现金");
            }
            one.put("ze", jyze);
            one.put("zb", DateUtils.getBFB(jyze / jye_total));
            jsonArray.add(one);
        }
        return jsonArray;
    }
}
